package com.chrray.position.config;

import com.chrray.position.enums.app.BaseEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;

/**
 * @author liujianxin
 **/
public class EnumsJsonSerializer extends JsonSerializer<BaseEnum> {


    @Override
    public void serialize(BaseEnum value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        // 读取字典缓存获取字典名称
        String info = value.getInfo();
        Integer code = value.getValue();
        // 原枚举不动
        jsonGenerator.writeObject(code);
        // 追加翻译后的名称
        String currentName = jsonGenerator.getOutputContext().getCurrentName();
        jsonGenerator.writeStringField(appendName(currentName),info);
    }

    private String appendName(String rawName) {
        return "_"+rawName;
    }
}
